﻿//Código desenvolvido por João Ribeiro
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.Configuration;

namespace GestaoContas
{
    public partial class Salas : Form
    {
        private Sala _sala;
        private Form _formPrincipal;
        private string _connectionStringJardimBD;
        private int _selectedRow;

        public Salas()
        {
            InitializeComponent();
        }


        public Salas(string tipoUser, Form FormPrincipal)
        {
            InitializeComponent();
            _sala = new Sala();
            _formPrincipal = FormPrincipal;
            _connectionStringJardimBD = ConfigurationManager.ConnectionStrings["ConnectionStringJardimBD"].ConnectionString;
            this.dataGridViewSalas.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dataGridViewSalas_MouseDown);

            PreencheGridViewSalas();

            if (tipoUser != "Administrador")
            {
                adicionarSalaToolStripMenuItem.Enabled = false;
                alterarSalaToolStripMenuItem.Enabled = false;
            }
        }

        /// <summary>
        /// Preenche a gridview das salas.
        /// </summary>
        private void PreencheGridViewSalas()
        {
            string query = @"SELECT NomeSala as ""Nome da Sala"", Observacoes as ""Observações"" FROM Sala";

            try
            {
                SqlCeConnection conn = new SqlCeConnection(_connectionStringJardimBD);
                SqlCeCommand cmd = new SqlCeCommand(query, conn);

                SqlCeDataAdapter da = new SqlCeDataAdapter();
                DataSet ds = new DataSet();

                da.SelectCommand = cmd;

                da.Fill(ds);
                dataGridViewSalas.DataSource = ds.Tables[0];
                dataGridViewSalas.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dataGridViewSalas_Click(object sender, EventArgs e)
        {
            PreencheGridViewSalas();
        }

        private void dataGridViewSalas_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Right && dataGridViewSalas.RowCount > 0)
                {
                    var hti = dataGridViewSalas.HitTest(e.X, e.Y);
                    dataGridViewSalas.ClearSelection();
                    dataGridViewSalas.Rows[hti.RowIndex].Selected = true;
                    _selectedRow = hti.RowIndex;
                }
            }
            catch (Exception ex) { }
        }

        private void adicionarSalaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SalaDialog addSala = new SalaDialog();
            addSala.Text = "Adicionar sala";
            if (addSala.ShowDialog() == DialogResult.OK)
            {
                Sala novaSala = new Sala(addSala.NomeSala, addSala.Observacoes);
                novaSala.RegistoSala(false);
                PreencheGridViewSalas();
            }
        }

        private void alterarSalaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridViewSalas.RowCount > 0)
            {
                string nomeSala = dataGridViewSalas.Rows[_selectedRow].Cells[0].Value.ToString();
                Sala sala = new Sala();
                sala.PreencheObjectoSala(nomeSala);

                SalaDialog alteraSala = new SalaDialog(sala.NomeSala, sala.Observacoes);
                alteraSala.Text = "Alterar sala";
                if (alteraSala.ShowDialog() == DialogResult.OK)
                {
                    sala.NomeSala = alteraSala.NomeSala;
                    sala.Observacoes = alteraSala.Observacoes;
                    sala.RegistoSala(true);
                    PreencheGridViewSalas();
                }
            }
        }

        private void verSalaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridViewSalas.RowCount > 0)
            {
                string nomeSala = dataGridViewSalas.Rows[_selectedRow].Cells[0].Value.ToString();

                VerSala verSala = new VerSala(nomeSala);
                verSala.MdiParent = _formPrincipal;
                verSala.Show();
            }
        }
    }
}
